<template>
  <div class="profile-container">
    <el-card class="profile-card">
      <template #header>
        <div class="card-header">
          <span>个人中心</span>
        </div>
      </template>
      
      <el-form :model="userForm" :rules="rules" ref="userForm" label-width="100px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="userForm.username" disabled></el-input>
        </el-form-item>
        
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="userForm.nickname"></el-input>
        </el-form-item>
        
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="userForm.email"></el-input>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="handleUpdate" :loading="loading">
            更新信息
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="profile-card">
      <template #header>
        <div class="card-header">
          <span>商品管理</span>
          <el-button type="primary" @click="$router.push('/products/manage')">
            管理我的商品
          </el-button>
        </div>
      </template>
      
      <div class="statistics">
        <el-row :gutter="20">
          <el-col :span="8">
            <div class="stat-item">
              <div class="stat-title">发布商品</div>
              <div class="stat-value">{{ statistics.totalProducts || 0 }}</div>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="stat-item">
              <div class="stat-title">在售商品</div>
              <div class="stat-value">{{ statistics.activeProducts || 0 }}</div>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="stat-item">
              <div class="stat-title">已售商品</div>
              <div class="stat-value">{{ statistics.soldProducts || 0 }}</div>
            </div>
          </el-col>
        </el-row>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'ProfilePage',
  data() {
    return {
      userForm: {
        username: '',
        nickname: '',
        email: ''
      },
      statistics: {
        totalProducts: 0,
        activeProducts: 0,
        soldProducts: 0
      },
      loading: false,
      rules: {
        nickname: [
          { required: true, message: '请输入昵称', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    await this.loadUserInfo()
    await this.loadStatistics()
  },
  methods: {
    async loadUserInfo() {
      const userInfo = await this.$store.dispatch('user/getUserInfo')
      this.userForm = { ...userInfo }
    },
    async loadStatistics() {
      try {
        // 调用获取统计数据的 API
        const stats = await this.$store.dispatch('user/getProductStatistics')
        this.statistics = stats
      } catch (error) {
        this.$message.error('获取统计信息失败')
      }
    },
    async handleUpdate() {
      this.$refs.userForm.validate(async valid => {
        if (valid) {
          this.loading = true
          try {
            await this.$store.dispatch('user/updateProfile', this.userForm)
            this.$message.success('更新成功')
          } catch (error) {
            this.$message.error(error.message || '更新失败')
          } finally {
            this.loading = false
          }
        }
      })
    }
  }
}
</script>

<style scoped>
.profile-container {
  max-width: 1200px;
  margin: 20px auto;
  padding: 0 20px;
}

.profile-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.statistics {
  padding: 20px 0;
}

.stat-item {
  text-align: center;
  padding: 20px;
  background-color: #f8f9fa;
  border-radius: 4px;
}

.stat-title {
  color: #606266;
  font-size: 14px;
  margin-bottom: 10px;
}

.stat-value {
  color: #303133;
  font-size: 24px;
  font-weight: bold;
}
</style> 